<template>
	<el-main>
		<el-card class="box-card" shadow="never">
			<div class="search-box">
				<el-form :inline="true" :model="searchForm" class="demo-form-inline">
					<el-form-item label="管理员昵称">
						<el-input v-model="searchForm.nickname" placeholder="" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit" icon="el-icon-search">查询</el-button>
					</el-form-item>
				</el-form>
			</div>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-bottom: 10px"></div>
			<el-button type="primary" icon="el-icon-plus" @click="addAdmin" style="margin-top: 10px">添加管理员
			</el-button>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-top: 10px"></div>
			<el-table
				:data="tableData"
				style="width: 100%;">
				<el-table-column
					prop="id"
					label="ID">
				</el-table-column>
				<el-table-column
					prop="code"
					label="编码">
				</el-table-column>
				<el-table-column
					prop="account"
					label="登录名">
				</el-table-column>
				<el-table-column
					prop="nickname"
					label="昵称">
				</el-table-column>
				<el-table-column
					prop="role.name"
					label="角色">
				</el-table-column>
				<el-table-column
					label="状态">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 1">正常</el-tag>
						<el-tag type="danger" v-if="scope.row.status == 2">禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column
					prop="create_time"
					label="创建时间">
				</el-table-column>
				<el-table-column
					prop="operation"
					label="操作">
					<template #default="scope">
						<el-button @click="handleEdit(scope.row)" type="text" size="small" v-if="scope.row.id > 1">编辑</el-button>
						<el-button @click="handleDel(scope.row)" type="text" size="small" v-if="scope.row.id > 1">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<Pages :form="searchForm" :page-obj="page" @pageChange="handlePageChange" @pageJump="getList"></Pages>

		</el-card>

		<save-dialog v-if="dialogVisible" ref="saveDialog" @success="handleSuccess" @closed="dialogVisible=false" :close-on-click-modal="false"></save-dialog>
	</el-main>
</template>

<script>
	import saveDialog from "@/views/admin/save";
	import Pages from "@/components/pages/index.vue";

	export default {
		name: 'admin',
		components: {
			Pages,
			saveDialog
		},
		data() {
			return {
				searchForm: {
					nickname: '',
					limit: 15,
					page: 1
				},
				dialogVisible: false,
				page: {
					total: 0
				},
				tableData: []
			}
		},
		mounted() {
			this.getList()
		},
		methods: {
			// 获取列表
			async getList() {
				let res = await this.$API.admin.list.get(this.searchForm)
				this.tableData = res.data.data
				this.page.total = res.data.total
			},
			// 搜索
			onSubmit() {
				this.searchForm.page = 1
				this.getList()
			},
			addAdmin() {
				this.title = '添加管理员'
				this.dialogVisible = true

				this.$nextTick(() => {
					this.$refs.saveDialog.open('add')
				});
			},
			// 编辑参数
			handleEdit(item) {
				this.title = '编辑管理员'
				this.dialogVisible = true

				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(item)
				});
			},
			// 删除参数
			handleDel(item) {
				this.$confirm('此操作将永久删除该管理员 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(async () => {
					let res = await this.$API.admin.del.get({id: item.id})
					if (res.code == 0) {
						this.$message.success(res.msg)
						this.getList()
					} else {
						this.$message.error(res.msg)
					}
				}).catch(() => {
				});
			},
			// 分页
			handlePageChange(page) {
				this.searchForm.page = page
				this.getList()
			},
			handleSuccess() {
				this.dialogVisible = false
				this.getList()
			}
		}
	}
</script>

<style scoped>
.el-button--small {
	padding: 0 !important;
}
</style>
